package com.cskaoyan.market.admin.controller;

import com.cskaoyan.market.admin.service.MarketAdminIssueService;
import com.cskaoyan.market.admin.service.impl.MarketAdminIssueServiceImpl;
import com.cskaoyan.market.db.domain.MarketIssue;
import com.cskaoyan.market.util.HttpUtil;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @Author:TaoRencan
 * @Date: 2023/3/21 9:41
 */
@WebServlet("/admin/issue/*")
public class AdminIssueController extends HttpServlet {
    //起手建立service层引用
    MarketAdminIssueService marketAdminIssueService = new MarketAdminIssueServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replace(request.getContextPath() + "/admin/issue", "");
        switch (op) {
            case "/delete":
                deleteIssue(request, response);
                break;
            case "/create":
                create(request, response);
                break;
            case "/update":
                update(request, response);
                break;
            default:
                response.setStatus(404);
        }

    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtil.getRequestBody(request);
        String answer = JacksonUtil.parseString(requestBody, "answer");
        String question = JacksonUtil.parseString(requestBody, "question");
        String addTime = JacksonUtil.parseString(requestBody, "addTime");
        String deleted = JacksonUtil.parseString(requestBody, "deleted");
        String idParam = JacksonUtil.parseString(requestBody, "id");
        String updateTime = JacksonUtil.parseString(requestBody, "updateTime");
        Integer id = Integer.parseInt(idParam);
        if (StringUtils.isEmpty(answer) || StringUtils.isEmpty(question)) {
            response.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        MarketIssue marketIssue = marketAdminIssueService.updateIssue(answer, question, addTime, deleted, id, updateTime);
        response.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok(marketIssue)));
    }

    private void create(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtil.getRequestBody(request);
        String answer = JacksonUtil.parseString(requestBody, "answer");
        String question = JacksonUtil.parseString(requestBody, "question");
        if (StringUtils.isEmpty(answer) || StringUtils.isEmpty(question)) {
            response.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        MarketIssue marketIssue = marketAdminIssueService.createIssue(answer, question);
        response.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok(marketIssue)));
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/issue", "");
        switch (op) {
            case "/list":
                list(req, resp);
                break;
            default:
                resp.setStatus(404);
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pageParam = req.getParameter("page");
        String limitParam = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String question = req.getParameter("question");
        if (StringUtils.isEmpty(pageParam) || StringUtils.isEmpty(limitParam)) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }
        int page = 0;
        int limit = 0;
        System.out.println(123);
        try {
            page = Integer.parseInt(pageParam);
            limit = Integer.parseInt(limitParam);
        } catch (Exception e) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        List<MarketIssue> marketIssueList = marketAdminIssueService.getList(page, limit, sort, order, question);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.okList(marketIssueList)));
    }

    private void deleteIssue(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jSONbody = HttpUtil.getRequestBody(request);
        // String jSONbody = byteArrayOutputStream.toString("utf-8");
        String id = JacksonUtil.parseString(jSONbody, "id");
        String question = JacksonUtil.parseString(jSONbody, "question");
        String deleted = JacksonUtil.parseString(jSONbody, "deleted");
        String updateTime = JacksonUtil.parseString(jSONbody, "updateTime");
        String answer = JacksonUtil.parseString(jSONbody, "answer");
        String addTime = JacksonUtil.parseString(jSONbody, "addTime");
        System.out.println(addTime);
        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(question) || StringUtils.isEmpty(deleted) || StringUtils.isEmpty(updateTime) || StringUtils.isEmpty(answer) || StringUtils.isEmpty(addTime)) {
            response.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        //简单校验之后，传递给service层处理
        marketAdminIssueService.delete(addTime, answer, deleted, id, question, updateTime);
        response.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok()));
    }


}

